//
//  ZDoctorPaperViewController.m
//  DoctorFAQs
//
//  Created by zjq on 17/2/21.
//  Copyright © 2017年 zjq. All rights reserved.
//

#import "ZDoctorPaperViewController.h"
#import "ZTacticsTableViewCell.h"

#import "ZTacticsDetailViewController.h"
#import "LZTacticsModel.h"
#import "DoctorModel.h"
static NSString * const ZDoctorPaperViewIdentifier = @"ZDoctorPaperViewIdentifier";
static int pPage = 1;
@interface ZDoctorPaperViewController ()<UITableViewDelegate ,UITableViewDataSource> {
    UITableView * _maintableView;
}
@property (nonatomic,strong) NSMutableArray <LZTacticsModel *>* LTModelArray;
@property (nonatomic,strong) NSMutableArray <DoctorModel *>* DModelArray;
@end

@implementation ZDoctorPaperViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initialzeDatas];
    [self initlizeInterface];
}
- (void)initialzeDatas {
    self.DModelArray =[@[] mutableCopy];
    self.LTModelArray = [@[] mutableCopy];
}

- (void)initlizeInterface {
    _maintableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_Width, SCREEN_Height) style:UITableViewStylePlain];
    _maintableView.delegate = self;
    _maintableView.dataSource = self;
    _maintableView.rowHeight = 260*ysIZE;
    [_maintableView registerClass:[ZTacticsTableViewCell class] forCellReuseIdentifier:ZDoctorPaperViewIdentifier];
    _maintableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        [self paperNetWorking];
        [_maintableView.mj_header endRefreshing];
    }];
    // 上拉加载
    _maintableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self moreNetWorking];
        [_maintableView.mj_footer endRefreshing];
    }];

    [self.view addSubview:_maintableView];
    [_maintableView.mj_header beginRefreshing];
}
// 刷新
- (void)paperNetWorking {
    pPage = 1;
    NSDictionary * dict = @{@"did":self.did,@"P":[NSString stringWithFormat:@"%d",pPage]};
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:dict paramarsSite:@"Consult/article" sucessBlock:^(id content) {
        NSDictionary  * aDict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        LZLog(@"%@",aDict);
        if ([aDict[@"code"] intValue] == 1 ) {
            [YJProgressHUD showMessage:@"刷行成功" inView:self.view];
                    [self hanldDataWith:aDict[@"list"]];
            
        }else {
            [YJProgressHUD showMessage:@"请求出错" inView:self.view];
        }

    } failure:^(NSError *error) {
        [YJProgressHUD showMessage:@"网络错误" inView:self.view];
    }];
}

//加载更多
- (void)moreNetWorking {
    pPage ++ ;
     NSDictionary * dict = @{@"did":self.did,@"P":[NSString stringWithFormat:@"%d",pPage]};
    [YJProgressHUD showProgress:@"正在刷新" inView:self.view];
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:dict paramarsSite:@"Consult/article" sucessBlock:^(id content) {
        NSDictionary * aDict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        LZLog(@"%@",aDict);
        if ([aDict[@"code"] intValue] == 1 ) {
            [YJProgressHUD showMessage:@"刷行成功" inView:self.view];
            [self moreHanldDataWith:aDict[@"list"]];
        }else {
            [YJProgressHUD showMessage:@"请求出错" inView:self.view];
        }

    } failure:^(NSError *error) {
        [YJProgressHUD showMessage:@"网络错误" inView:self.view];
    }];
}

- (void)hanldDataWith:(NSArray * )array {
    [_maintableView.mj_footer resetNoMoreData];
    [self.LTModelArray removeAllObjects];
    [self.DModelArray removeAllObjects];
    if (array.count == 0) {
        [YJProgressHUD showMessage:@"已无更多案例" inView:self.view];
        [_maintableView.mj_footer endRefreshingWithNoMoreData];
        
    }else if (array.count < 10) {
        [YJProgressHUD showMessage:@"刷新成功" inView:self.view];
        [_maintableView.mj_footer endRefreshingWithNoMoreData];
    }
    for (NSMutableDictionary * dict in array) {
        LZTacticsModel * model = [[LZTacticsModel alloc] initWithDict:dict];
        DoctorModel * dmodel = [[DoctorModel alloc] initWithDict:dict];
        [self.LTModelArray addObject:model];
        [self.DModelArray addObject:dmodel];
    }
    [_maintableView reloadData];
}
- (void)moreHanldDataWith:(NSArray * )array {
    [_maintableView.mj_footer resetNoMoreData];
    [self.LTModelArray removeAllObjects];
    [self.DModelArray removeAllObjects];
    if (array.count == 0) {
        [YJProgressHUD showMessage:@"已无更多案例" inView:self.view];
        [_maintableView.mj_footer endRefreshingWithNoMoreData];
        return;
    }else if (array.count < 10) {
        [YJProgressHUD showMessage:@"刷新成功" inView:self.view];
        [_maintableView.mj_footer endRefreshingWithNoMoreData];
    }
    for (NSMutableDictionary * dict in array) {
        LZTacticsModel * model = [[LZTacticsModel alloc] initWithDict:dict];
        DoctorModel * dmodel = [[DoctorModel alloc] initWithDict:dict];
        [self.LTModelArray addObject:model];
        [self.DModelArray addObject:dmodel];
    }
    [_maintableView reloadData];
}

// 表格视图协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return (self.LTModelArray&&self.LTModelArray.count !=0) ? self.LTModelArray.count:0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZTacticsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ZDoctorPaperViewIdentifier forIndexPath:indexPath];
    cell.tacticsModel = self.LTModelArray[indexPath.row];
    cell.doctorModel = self.DModelArray[indexPath.row];
    cell.selectionStyle = 0 ;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self jumpWenZhangXQVCWithRow:indexPath.row];
}

- (void)jumpWenZhangXQVCWithRow:(NSInteger)row {
    ZTacticsDetailViewController * ZTVC = [[ZTacticsDetailViewController alloc] init];
    ZTVC.artId = self.LTModelArray[row].artID;
    [self.navigationController pushViewController:ZTVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
